//
//  OAuthViewController.swift
//  weibo
//
//  Created by apple on 16/2/20.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking


class OAuthViewController: UIViewController {
    
    //设置属性webview
    let webView = UIWebView()
    
    //关闭视图控制器
    @objc private func close(){
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func testAccount() {
        
        let jsString = "document.getElementById('userId').value = '15713978239' , document.getElementById('passwd').value = 'Aa15093117092' "
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    override func loadView() {
        view = webView
        //设置web代理
        webView.delegate = self
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "测试账号", style: .Plain, target: self, action: "testAccount")
        //使用webView加载授权页面
        loadOauthPage()
        
    }
    //在页面将要消失的时候
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        SVProgressHUD.dismiss()
    }
    
    //加载授权页面
    private func loadOauthPage() {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=" + client_id + "&redirect_uri=" + redirect_uri
        let url = NSURL(string: urlString)!
        let request = NSURLRequest(URL: url)
        webView.loadRequest(request)
        
    }
    
}
//处理webView的协议方法
extension OAuthViewController: UIWebViewDelegate {
    //开启指示器
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    //关闭指示器
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        guard let urlString = request.URL?.absoluteString else {
            return false
        }
        
        if urlString.containsString("code") {
        
            guard let query = request.URL?.query else {
            return false
            }
            
            let codeStr = "code="
            let code = (query as NSString).substringFromIndex(codeStr.characters.count)
            
            AccountViewModel.sharedAccountViewModel.loadAccessToken(code, finished: { (isSuccess) -> () in
                if isSuccess {
                    self.dismissViewControllerAnimated(true, completion: nil)
                    
                    NSNotificationCenter.defaultCenter().postNotificationName(KChoseRootVC, object: nil)
         
                
                } else {
                    //登录失败
                    SVProgressHUD.showErrorWithStatus("登录失败，请检查网络")
                
                }
        
            })
       
        return false
        }
        return true
    }
}












